import json
import logging
import datetime


class KafkaLogger:
    """系统审计信息"""
    AuditTopic = 'system_audit_log1'
    WarnTopic = 'system_warn_log1'

    def __init__(self):
        self.confluent_kafka = None

    def init_config(self, confluent_kafka):
        self.confluent_kafka = confluent_kafka

    def info(self, **kwargs):
        kwargs['_time'] = int(datetime.datetime.utcnow().timestamp() * 1000)
        self.confluent_kafka.send(self.AuditTopic, json.dumps(kwargs, ensure_ascii=False))

    def error(self, **kwargs):
        kwargs['_time'] = int(datetime.datetime.utcnow().timestamp() * 1000)
        self.confluent_kafka.send(self.WarnTopic, json.dumps(kwargs, ensure_ascii=False))


class StreamLogger:
    """流式日志"""
    def __init__(self):
        self.logger = None

    def init_config(self, name, level='INFO'):
        self.logger = logging.getLogger(name)
        self.logger.setLevel(level)
        if not self.logger.handlers:
            self.logger.addHandler(logging.StreamHandler())


class HttpLogger(StreamLogger):
    """http请求日志记录"""
    pass


auditor_logger = KafkaLogger()
http_logger = HttpLogger()
